﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

using Microsoft.Phone.Tasks;

namespace SmartHYU_TestVersion.News
{
    public partial class HyuNews : PhoneApplicationPage
    {
        HyNews myNews = new HyNews();

        public HyuNews()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            // for Offiline Mode - Load local data
            myNews.LoadAndParseToHyNewsForm();
            newsListBox.ItemsSource = myNews.HyNewsList;

            // Data Loading... 
            newsDownloadProgressBar.Visibility = System.Windows.Visibility.Visible;
            newsDownloadProgressBar.IsIndeterminate = true;

            myNews.DownloadNewsAndStore();

            myNews.NewsDownloadCompleted += new HyNews.NewsDownloadStatus(myNews_NewsDownloadCompleted);
        }

        void myNews_NewsDownloadCompleted(bool completedStatus, string rssString)
        {
            myNews.LoadAndParseToHyNewsForm();
            newsListBox.ItemsSource = myNews.HyNewsList;

            newsDownloadProgressBar.IsIndeterminate = false;
            newsDownloadProgressBar.Visibility = System.Windows.Visibility.Collapsed;
        }

        // When click content provider :
        private void TextBlock_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            WebBrowserTask hyNewsWeb = new WebBrowserTask();
            hyNewsWeb.URL = "http://hynews.ac.kr/";
            hyNewsWeb.Show();
        }

        // When click article
        private void newsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string articleUrl = myNews.HyNewsList[newsListBox.SelectedIndex].Url;

            WebBrowserTask articleViewer = new WebBrowserTask();
            articleViewer.URL = articleUrl;
            articleViewer.Show();
        }
    }
}